/**
 * 报销单打开订单选择
 * 注：有些元素不太清楚，暂时不封装了
 * @author hongwei 2018-09-12
 */
(function (window) {
    'use strict';
    var expenseChoose = function(params) {
        $.extend(this.params, params);
        var that = this;
        that._init();   //初始化操作
    };
    expenseChoose.prototype = {
        params : {

        }

        //初始化操作
        ,_init : function () {
            var that = this;
            that.clickGetList();     //step1:点击获取订单列表信息，包含step2
            //that.getDataList();    //step2:获取订单列表
            that.confirmChoose();    //step3:点击选择确定
            that.dateScroll();       //step4:对日期的操作
        }

        //step1:点击获取订单列表信息
        ,clickGetList : function () {
            var that = this;
            $('#getDataList').click(function () {
                that.getDataList();
            });
        }

        //step2:获取订单列表
        ,getDataList : function () {
            $("#order_list").text('');
            var toast = new auiToast();
            //当前页数
            var page=0;
            var page_size=10;
            var page_max=0;
            $('#order_list').dropload({
                scrollArea : window,
                domDown: {
                    domNoData: '<div class="dropload-noData">已经到底了</div>',
                    domRefresh : ''
                },
                loadDownFn : function(me){
                    if(page > page_max){
                        me.lock();
                        me.noData();
                        me.resetload();
                        return false;
                    }
                    ++page;
                    //获取提交数据
                    var order_type = $("#order_type").val();
                    var start_time = $("#start_time").val();
                    var end_time = $("#end_time").val();

                    var param = {};
                    param.url = Config.travel.getOrderList;
                    param.data = {page: page, limit: page_size, start_time: start_time, end_time: end_time, product_id: order_type};
                    // console.log(param);
                    ajax(param,function(ret){
                        // console.log(ret);
                        if(ret.code === 1){
                            if(ret.data.data.length > 0){
                                //插入模板
                                var interText = doT.template($("#list_tpl").text());
                                $("#order_list").append(interText(ret.data.data));
                                page = ret.data.current_page;
                                page_max = parseInt(ret.data.last_page);
                            }else	{
                                me.lock();
                                me.noData();
                            }
                            me.resetload();
                        } else {
                            toast.custom({
                                title:"获取订单失败",
                                html:'<i class="aui-iconfont aui-icon-info"></i>',
                                duration:2000
                            });
                            me.resetload();
                        }
                    })
                }
            });
        }
        
        //step3:点击选择确定
        //todo  对checkbox点击有问题
        ,confirmChoose : function (){
            var toast = new auiToast();
            $('body').on('click', '.aui-checkbox', function () {
                var orderids = '';
                console.log(orderids);
                $(".aui-checkbox:checked").each(function(){
                    orderids += $(this).val()+',';
                });
                if(orderids === ''){
                    toast.custom({
                        title:"请选择订单",
                        html:'<i class="aui-iconfont aui-icon-info"></i>',
                        duration:2000
                    });
                    return false;
                }
                orderids = orderids.substr(0, orderids.length-1);
                $api.setStorage('orderids', orderids);
                console.log(orderids);
            });
        }

        //step4:对日期的操作
        // todo 这里没有对这个进行操作
        ,dateScroll : function () {
            $('#start_time').mobiscroll().date({
                theme: 'mobiscroll',
                lang: 'zh',
                dateFormat: 'yyyy-mm-dd',
                display: 'center',
                mode: 'Basic usage date'
            });
            $('#end_time').mobiscroll().date({
                theme: 'mobiscroll',
                lang: 'zh',
                dateFormat: 'yyyy-mm-dd',
                display: 'center',
                mode: 'Basic usage date'
            });
        }
    };
    window.expenseChoose =  expenseChoose;
})(window);